/*
 * GUI.java
 *
 * 
 */
package it.luigiarpino.alt2jsim.gui;

import it.luigiarpino.alt2jsim.core.Grafo;
import it.luigiarpino.alt2jsim.core.ParseGrafo;
import it.luigiarpino.alt2jsim.netpanel.FlatPanel;
import it.luigiarpino.alt2jsim.netpanel.MultiLevelPanel;
import it.luigiarpino.alt2jsim.netpanel.TransitStubPanel;
import javax.swing.*;
import java.io.File;
import java.awt.*;

/**
 *
 * @author  Luigi Lorenzo Arpino 534/559
 */
public class GUI extends JFrame{
    
    /**
	 * 
	 */
	private static final long serialVersionUID = 5810182397071127627L;
	private JMenuBar barraMenu;
    private JMenu menuFile;
    private JMenuItem menuItemEsci;
    private JMenuItem menuItemImporta;
    private JMenuItem menuItemEsportaToJsim;
    private JMenuItem menuItemEsportaToNeptune;
    private Grafo grafo;
    private JPanel pannello;
    private JPanel netPanel;
    private JLabel netLabel;
    private JMenu jMenuInfo;
    private JMenuItem jMenuItemAbout;
    
    /** Creates a new instance of GUI */
    public GUI() {
        try{ 
            javax.swing.UIManager.setLookAndFeel(javax.swing.UIManager.getSystemLookAndFeelClassName());
    	} 
    	catch(Exception e) { }
        this.addWindowListener(new java.awt.event.WindowAdapter() {
            public void windowClosing(java.awt.event.WindowEvent evt) {
                exitForm(evt);
            }
        });
        this.pannello=new JPanel();
        this.pannello.setLayout(new BorderLayout());
        this.getContentPane().add(this.pannello);
        this.barraMenu=new JMenuBar();
        this.menuFile=new JMenu("File");
        this.menuItemEsci=new JMenuItem("Quit");
        this.menuItemEsci.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                Menu_Esci(evt);
            }
        });
        this.menuItemImporta=new JMenuItem("Import gt-itm ALT file format");
        this.menuItemImporta.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                Menu_Importa(evt);
            }
        });
        this.menuItemEsportaToJsim=new JMenuItem("Export to JSim TCL file format");
        this.menuItemEsportaToJsim.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                Menu_Esporta(evt);
            }
        });
        this.menuItemEsportaToJsim.setEnabled(false);
        // Menu esporta tu neptune
        this.menuItemEsportaToNeptune=new JMenuItem("Export to Neptune XML file format");
        this.menuItemEsportaToNeptune.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                Menu_EsportaToNeptune(evt);
            }
        });
        this.menuItemEsportaToNeptune.setEnabled(false);
        
        this.menuFile.add(this.menuItemImporta);
        this.menuFile.add(this.menuItemEsportaToJsim);
        this.menuFile.add(this.menuItemEsportaToNeptune);
        this.menuFile.addSeparator();
        this.menuFile.add(this.menuItemEsci);
        this.jMenuInfo=new JMenu("Info");
        this.jMenuItemAbout=new JMenuItem("About");
        this.jMenuInfo.add(this.jMenuItemAbout);
        this.jMenuItemAbout.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                Menu_About(evt);
            }
        });
        this.barraMenu.add(this.menuFile);
        this.barraMenu.add(this.jMenuInfo);
        this.setJMenuBar(this.barraMenu);
        this.setTitle("Alt2JSim");
        //this.setExtendedState(JFrame.MAXIMIZED_BOTH);
        
    }
    
    private void Menu_Esci(java.awt.event.ActionEvent evt) {
        System.exit(0);
    }
    
    private void Menu_Esporta(java.awt.event.ActionEvent evt) {
        try
        {
        	GuiEsportaToJsim guiEsporta = new GuiEsportaToJsim(this.grafo);
        	guiEsporta.setVisible(true);
        	guiEsporta.setLocationRelativeTo(null);
        }
        catch(java.lang.Exception e)
        {
            JOptionPane.showMessageDialog(this,"Unable to export network!","Error",JOptionPane.ERROR_MESSAGE );
        }
    }
    
    private void Menu_EsportaToNeptune(java.awt.event.ActionEvent evt) 
    {
    	try
        {
        	GuiEsportaToNeptune guiEsporta = new GuiEsportaToNeptune(this.grafo);
        	guiEsporta.setVisible(true);
        	guiEsporta.setLocationRelativeTo(null);
        }
        catch(java.lang.Exception e)
        {
        	e.printStackTrace();
            JOptionPane.showMessageDialog(this,"Unable to export network!","Error",JOptionPane.ERROR_MESSAGE );
        }
    }
    
    private void Menu_About(java.awt.event.ActionEvent evt) {
        String messaggio=new String();
        messaggio="<HTML> Software developed by Luigi Lorenzo Arpino<BR>" +
        "<A href='l.arpino@studenti.unina.it'>l.arpino@studenti.unina.it</A> <BR>" +
        "</HTML>";
        JOptionPane.showMessageDialog(null,messaggio,"Information",JOptionPane.INFORMATION_MESSAGE );
    }
    
    private void Menu_Importa(java.awt.event.ActionEvent evt) {
        try{
            JFileChooser f=new JFileChooser();
            f.showDialog(new JFrame(),"Importa");  
            File importFile=f.getSelectedFile();
            if (importFile!=null){
                ParseGrafo par=new ParseGrafo(importFile.getAbsolutePath());
                this.grafo=par.getGrafo();
                if (this.grafo!=null){
                    if (this.netPanel!=null){
                        this.pannello.remove(this.netPanel);
                    }
                    if (this.netLabel!=null){
                        this.pannello.remove(this.netLabel);
                    }
                    this.VisualizzaRete();
                    this.menuItemEsportaToJsim.setEnabled(true);
                    this.menuItemEsportaToNeptune.setEnabled(true);
                    this.setTitle("Alt2JSim  - "+importFile.getName());
                }
                else{
                    JOptionPane.showMessageDialog(null,par.getErrorString(),"Attenzione",JOptionPane.ERROR_MESSAGE );
                }  
            }// fine if
        }// fine blocco try
        catch(java.lang.Exception e){
            JOptionPane.showMessageDialog(null,"Impossibile Importare la rete","Attenzione",JOptionPane.ERROR_MESSAGE );
        }
    }
    
    
    private void VisualizzaRete(){
        try{
            this.netLabel=new JLabel();
            this.netLabel.setFont(new Font("DIALOG",Font.PLAIN,14));
            if (this.grafo.getTypeOfGraph().equalsIgnoreCase(Grafo.TYPE_FLAT)){
                this.netPanel=new FlatPanel(this.grafo);
                this.pannello.add(this.netPanel,BorderLayout.CENTER);
            }
            else if(this.grafo.getTypeOfGraph().equalsIgnoreCase(Grafo.TYPE_TRANSIT)){
                this.netPanel=new TransitStubPanel(this.grafo);
                this.pannello.add(this.netPanel,BorderLayout.CENTER);
            }
            else{
                this.netPanel=new MultiLevelPanel(this.grafo);
                this.pannello.add(this.netPanel,BorderLayout.CENTER);
            }
            this.validateTree();
        }// fine blocco try
        catch(java.lang.Exception e){
            JOptionPane.showMessageDialog(null,"Impossibile visualizzare la rete","Attenzione",JOptionPane.ERROR_MESSAGE );
        }
    }// fine visualizzaRete()
        
    private void exitForm(java.awt.event.WindowEvent evt) {
        System.exit(0);
    }
    
    /**
     * @param args the command line arguments
     */
    public static void main(String[] args)
    {
    	GUI gui = new GUI();
        gui.setVisible(true);
        gui.setSize(800, 600);
        gui.setLocationRelativeTo(null);
    }
    
}
